<?php

namespace MoChat\App\Home\Action;

use Hyperf\DbConnection\Db;
use MoChat\App\Home\Contract\CorpContract;
use MoChat\App\Home\Contract\RbacRoleContract;
use MoChat\App\Home\Contract\TenantContract;
use MoChat\App\Sales\Contract\WorkEmployeeContract;
use MoChat\App\WorkContact\Action\Sidebar\EmployeeContact;
use MoChat\Framework\Action\AbstractAction;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\Sales\Constants\SalesTaskStatusCode;
use MoChat\App\Sales\Contract\SalesClientTaskContract;
use MoChat\App\Sales\Contract\SalesClientTaskEmployeeContract;
use MoChat\App\Sales\Contract\SalesTaskContract;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Constants\ErrorCode;

/**
 * 账号信息
 * @Controller
 */
class User extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var CorpContract
     */
    protected $corpService;

    /**
     * @Inject
     * @var RbacRoleContract
     */
    protected $rbacRoleService;

    /**
     * @Inject
     * @var WorkEmployeeContract
     */
    protected $workEmployeeService;

    /**
     * @Inject
     * @var TenantContract
     */
    protected $tenantService;


    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     * })
     * @RequestMapping(path="/dashboard/home/user", methods="GET")
     */
    public function handle()
    {
        $user = user();
        // 判断用户绑定企业信息
        if (!isset($user['corpIds']) || count($user['corpIds']) !== 1) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '未选择登录企业，不可操作');
        }
        //企业信息
        $corp = $this->corpService->getDetailCorp($user['corpIds'][0], [
            'name',
            'tenant_id'
        ]);
        if (empty($corp)){
            throw new CommonException(ErrorCode::INVALID_PARAMS, '未配置企业，不可操作');
        }
        //角色名称
        $role = $this->rbacRoleService->getDetailCorp($user['roleId'], [
            'name',
        ]);
        //头像
        $employee = $this->workEmployeeService->getDetail($user['workEmployeeId'], [
            'avatar'
        ]);
        //到期时间
        $tenant = $this->tenantService->getDetailById($corp['tenantId'], ['expired_at']);
        return [
            'name' => $user['name'],//员工名称
            'corpName' => $corp['name'] ?? '',//企业名称
            'roleName' => $role['name']??'超级管理员',//角色名称
            'avatar' => $employee['avatar']??'',//头像
            'employeeNumber' => Db::table('work_employee')->where('corp_id',$user['corpIds'][0])->count(),//员工人数
            'expiredAt' => $tenant['expiredAt'],//到期时间
        ];
    }

    /**
     * 验证规则.
     */
    protected function rules(): array
    {
        return [
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    protected function messages(): array
    {
        return [
        ];
    }
}